// pages/edit_business_info/edit_business_info.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), WeValidator = require('../../utils/we-validator');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: {},
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    core.get({
      url: '/enterprise/info',
      success: ret => {
        let info = ret.data.enterprise_info;
        info.environment_images = info.environment_images ? info.environment_images.split(',') : [];
        this.setData({
          info: info,
          audit_status: ret.data.audit_status,
          personal_audit_status: ret.data.personal_audit_status,
        });
        util.getCompanyTypes().then(types=>{
          this.setData({companyTypes: types});
        });
        util.getCompanyScales().then(scales=>{
          this.setData({companyScales: scales});
        })
      },
      fail: ret => {
        wx.navigateBack();
      }
    });
  },
  bindEditBasicInfo() {
    wx.navigateTo({
      url: '/pages/business_info/business_info',
      events: {
        infoTransfer: data => {
          this.setData({info: data.info});
          this.bindSave(false);
        }
      },
      success: res => {
        res.eventChannel.emit('infoTransfer', {info:this.data.info});
      }
    });
  },
  bindCompanyTypeChange(event) {
    let selectedCompanyType = event.detail.items[0];
    this.setData({'info.company_type_id': selectedCompanyType.id});
  },
  bindCompanyScaleChange(event) {
    let selectedCompanyScale = event.detail.items[0];
    this.setData({'info.company_scale_id': selectedCompanyScale.id});
  },
  bindEditLabel() {
    wx.navigateTo({
      url: '/pages/show_label/show_label',
      events: {
        infoTransfer: data => {
          this.setData({info: data.info});
          this.bindSave(false);
        }
      },
      success: res => {
        res.eventChannel.emit('infoTransfer', {info:this.data.info});
      }
    });
  },
  bindEditIntro() {
    wx.navigateTo({
      url: '/pages/business_edit/business_edit',
      events: {
        infoTransfer: data => {
          this.setData({info: data.info});
        }
      },
      success: res => {
        res.eventChannel.emit('infoTransfer', {info:this.data.info});
      }
    });
  },
  bindEditEnvironmentImages() {
    wx.navigateTo({
      url: '/pages/environment/environment',
      events: {
        infoTransfer: data => {
          this.setData({info: data.info});
          this.bindSave(false);
        }
      },
      success: res => {
        res.eventChannel.emit('infoTransfer', {info:this.data.info});
      }
    });
  },
  bindEditEnterpriseAudit() {
    wx.navigateTo({
      url: '/pages/attestation/attestation',
      // success: res => {
      //   res.eventChannel.emit('infoTransfer', {info:this.data.info, status:this.data.personal_audit_status});
      // }
    });
  },
  bindEditPersonalAudit() {
    wx.navigateTo({
      url: '/pages/person_attestation/person_attestation',
      events: {
        infoTransfer: data => {
          this.setData({info: data.info, personal_audit_status:data.status});
        }
      },
      success: res => {
        res.eventChannel.emit('infoTransfer', {info:this.data.info, status:this.data.personal_audit_status});
      }
    });
  },
  bindSave(navigateBack=true) {
    let keys = ['full_name','industry_category_id','contact_person','contact_phone','contact_email','address','logo',
      'company_scale_id','company_type_id','intro','environment_images'];
    let postData = {};
    keys.forEach(key => postData[key] = this.data.info[key]);
    postData.environment_images = postData.environment_images.join(',');
    let benefitIds = [];
    this.data.info.benefits.forEach(item=>benefitIds.push(item.id));
    postData.benefit_ids = benefitIds.join(',');
    core.post({
      url: 'enterprise/save_info', data: postData,
      success: ret => {
        app.eventBus.emit(app.Events.UcenterInfoCompleteChange, ret.data.enterprise_info.info_complete);
        if(navigateBack)
          wx.navigateBack();
      }
    });
  }
});
